package fr.inserm.u915.organifooyi.model;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;



@Entity(name="extraction")
@Table(name="extraction")
@NamedQueries({
    @NamedQuery(name="extraction.list.all",
            query="select E from extraction as E ")
   	})

public class Extraction extends AbstractUpdatableObject
	{
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="reception_id")
	private ReceptionTubes reception;
	@Column(length=1,name="symbol",nullable=false)
	private Character symbol;
	@OneToMany(mappedBy="extraction",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private List<Tube> tubes=new ArrayList<Tube>();
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="protocol_id")
	private Protocol protocol;
	@OneToMany(mappedBy="extraction",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private List<Aliquot> aliquots=new ArrayList<Aliquot>();

	
	public ReceptionTubes getReception()
		{
		return reception;
		}
	
	public void setReception(ReceptionTubes reception)
		{
		this.reception = reception;
		}
	
	public Protocol getProtocol()
		{
		return protocol;
		}
	
	public void setProtocol(Protocol protocol)
		{
		this.protocol = protocol;
		}
	
	public Character getSymbol()
		{
		return symbol;
		}
	
	public void setSymbol(Character symbol)
		{
		this.symbol = symbol;
		}
	
	public List<Tube> getTubes()
		{
		return tubes;
		}
	public void setTubes(List<Tube> tubes)
		{
		this.tubes = tubes;
		}
	
	public List<Aliquot> getAliquots()
		{
		return aliquots;
		}
	
	public void setAliquots(List<Aliquot> aliquots)
		{
		this.aliquots = aliquots;
		}
	
	@Transient
	public Individual getIndividual()
		{
		ReceptionTubes r=getReception();
		if(r==null) return null;
		return r.getIndividual();
		}
	
	@Transient
	public String getLabel()
		{
		if(getReception()==null) return "Extraction.ID"+getId();
		return getReception().getCdNumber()+"_"+getSymbol();
		}
	
	@Override
	public String toString() {
		return getLabel();
		}
	}
